package com.share.controller.manager;


import cn.hutool.core.util.IdUtil;
import com.share.pojo.domain.Systemdictionary;
import com.share.result.JSONResult;
import com.share.template.CosTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Tag(name = "cos",description = "cos")
@RestController
@RequestMapping("/manager/cos")
public class CosController {
    @Autowired
    private CosTemplate cosTemplate;
    @Operation( summary= "upload",description = "图片上传接口")
    @Parameter(name = "upload",description = "图片上传接口",required = true)
    @PostMapping("/upload")
    public JSONResult upload(@RequestParam("file") MultipartFile multipartFile){
        try {
            String originalFilename = multipartFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //上传后的文件名
            String uploadFileName = UUID.randomUUID().toString()+suffix;
            String upload = cosTemplate.upload(multipartFile.getInputStream(), uploadFileName);
            return JSONResult.success(upload);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
